using System;
using System.Collections;
using System.Text;
using System.Web;
using Sedna.Core.Common.Objects.Navigation;
using Iesi.Collections;

namespace Sedna.Core.Controller.Common
{
    public abstract class AbstractGridValidationAction : AbstractValidationAction
    {
        private string transientDataPath;
        private string invalidDataPath;
        private string markedDataPath;
        private string errorMessageKeyDataPath;
        
        #region Properties
        
        protected ISet TransientData
        {
            get
            {
                return scopePathParser.Parse(transientDataPath).Value as ISet;
            }
        }

        protected ISet InvalidData
        {
            get
            {
                return scopePathParser.Parse(invalidDataPath).Value as ISet;
            }
        }

        public ISet MarkedData
        {
            get
            {
                return scopePathParser.Parse(markedDataPath).Value as ISet;
            }
        }
        
        public IDictionary ErrorMessageKeyData
        {
            get { return scopePathParser.Parse(errorMessageKeyDataPath).Value as IDictionary; }
        }
        
        public string TransientDataPath
        {
            set { transientDataPath = value;}
        }

        public string InvalidDataPath
        {
            set { invalidDataPath = value;}
        }

        public string MarkedDataPath
        {
            set { markedDataPath = value; }
        }

        public string ErrorMessageKeyDataPath
        {
            set { errorMessageKeyDataPath = value; }
        }

        #endregion

        public override IActionResult Execute(IExecutionContext executionContext)
        {
            SetIsValid(executionContext, false);
            
            return CreateResult(successStatus);
        }

        protected void AddToInvalidData(object o)
        {
            if (!InvalidData.Contains(o))
                InvalidData.Add(o);
        }

        protected void AddToInvalidData(ISet set)
        {
            foreach (Object o in set)
                AddToInvalidData(o);
        }

        protected bool IsValid(object o)
        {
            return !InvalidData.Contains(o);
        }

        protected void AddToErrorMessageKeyData(object o, string messageKey, params object[] args)
        {
            messageKey = messageKey.Trim();

            IDictionary messages = ErrorMessageKeyData[o] as IDictionary;
            
            if (messages == null)
            {
                messages = new Hashtable();
                ErrorMessageKeyData[o] = messages;                
            }
            
            if (!messages.Contains(messageKey))
            {
                messages.Add(messageKey, args);
            }                        
        }
        
        protected void AddToErrorMessageKeyData(ICollection collection, string messageKey, params object[] args)
        {
            foreach (Object o in collection)
            {
                AddToErrorMessageKeyData(o, messageKey, args);
            }
        }
    }
}
